import {
  ssoLogin,
  login,
  logout,
  getInfo
} from '@/api/login/login'
import {
  getToken,
  setToken,
  removeToken,
  setType
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

const state = {
  token: getToken(),
  name: '',
  username: '',
  avatar: '',
  mobile: '',
  email: '',
  roles: '',
  rights: [],
  btnRights: [],
  mainDeptId: '',
  mainDeptName: '',
  mainDeptPost: '',
  doorId: '',
  id: null
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_USERNAME: (state, username) => {
    state.username = username
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MOBILE: (state, mobile) => {
    state.mobile = mobile
  },
  SET_EMAIL: (state, email) => {
    state.email = email
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_RIGHTS: (state, rights) => {
    state.rights = rights
  },
  SET_BTN_RIGHTS: (state, btnRights) => {
    state.btnRights = btnRights
  },
  SET_DOOR_ID: (state, doorId) => {
    state.doorId = doorId
  },
  SET_ID: (state, id) => {
    state.id = id
  },
  SET_MAINDEPTID: (state, mainDeptId) => {
    state.mainDeptId = mainDeptId
  },
  SET_MAINDEPTNAME: (state, mainDeptName) => {
    state.mainDeptName = mainDeptName
  },
  SET_MAINDEPTPOST: (state, mainDeptPost) => {
    state.mainDeptPost = mainDeptPost
  }
}

const actions = {
  // user login
  ssoLogin({
    commit
  }, userInfo) {
    const {
      code,
      state
    } = userInfo
    commit('SET_TOKEN', '')
    commit('SET_ROLES', [])
    commit('SET_RIGHTS', [])
    commit('SET_BTN_RIGHTS', [])
    commit('SET_DOOR_ID', '')
    removeToken()
    resetRouter()
    return new Promise((resolve, reject) => {
      ssoLogin({
        code: code,
        state: state
      }).then(response => {
        setType('user')
        const {
          data
        } = response
        let {
          access_token,
          token_type
        } = data
        access_token = token_type + ' ' + access_token
        commit('SET_TOKEN', access_token)
        setToken(access_token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      login({
        username: username.trim(),
        password: password
      }).then(response => {
        setType('user')
        const {
          data
        } = response
        let {
          access_token,
          token_type
        } = data
        access_token = token_type + ' ' + access_token
        commit('SET_TOKEN', access_token)
        setToken(access_token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const {
          data
        } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }
        data.roles = ['user']
        const {
          username,
          name,
          mobile,
          email,
          roles = ['user'],
          rights,
          btnRights,
          door_id,
          id,
          mainDeptId,
          mainDeptName,
          mainDeptPost
        } = data
        commit('SET_NAME', name)
        commit('SET_USERNAME', username)
        commit('SET_MOBILE', mobile)
        commit('SET_EMAIL', email)
        commit('SET_ROLES', roles)
        commit('SET_RIGHTS', rights || [])
        commit('SET_BTN_RIGHTS', btnRights || [])
        commit('SET_DOOR_ID', door_id)
        commit('SET_ID', id)
        commit('SET_MAINDEPTID', mainDeptId)
        commit('SET_MAINDEPTNAME', mainDeptName)
        commit('SET_MAINDEPTPOST', mainDeptPost)
        // commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        commit('SET_RIGHTS', [])
        commit('SET_BTN_RIGHTS', [])
        commit('SET_DOOR_ID', '')
        commit('SET_ID', '')
        commit('permission/SET_ROUTES', [], { root: true })
        commit('tagsView/DEL_ALL_VISITED_VIEWS', [], { root: true })
        commit('tagsView/DEL_ALL_CACHED_VIEWS', [], { root: true })
        commit('SET_MAINDEPTID', '')
        commit('SET_MAINDEPTNAME', '')
        commit('SET_MAINDEPTPOST', '')
        removeToken()
        resetRouter()
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
